package com.thfw.test.view.activity;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.Image;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Button;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.bigkoo.pickerview.TimePickerView;

import android.content.DialogInterface;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.thfw.test.model.authManage.AuthBaseBean;
import com.thfw.test.model.authManage.LampInterfaceBean;
import com.thfw.test.model.bean.BindingBean;
import com.thfw.test.model.bean.GroupAdapter;
import com.thfw.test.model.bean.GroupBean;
import com.thfw.test.model.bean.LampListBean;
import com.thfw.test.model.bean.LightAdapter;
import com.thfw.test.model.bean.MainEnergyBean;
import com.thfw.test.model.bean.OnItemCallBack;
import com.thfw.test.model.bean.RegionBean;
import com.thfw.test.utils.ConfigurationEntry;
import com.thfw.test.utils.StringUtils;
import com.thfw.test.view.mq.MessageModel;
import com.thfw.test.view.mq.PahoHepler;
import com.thfw.test.view.network.AuthManageService;
import com.thfw.wmd.R;
import com.thfw.test.utils.ToastUtil;
import com.thfw.test.utils.ToastUtils;
import com.thfw.test.view.fragment.AboutMeFragment;
import com.thfw.test.view.fragment.DeviceManagementFragment;
import com.thfw.test.view.fragment.EnergyManagementFragment;
import com.thfw.test.view.fragment.LightControlFragment;
import com.thfw.test.view.widget.LineChart02View;

import butterknife.BindView;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import scooper.cn.sc_base.log.SCLog;
import utils.AppActivities;
import utils.RetrofitWrapper;
import utils.ViewSelectGroup;

import org.json.JSONArray;
import org.json.JSONObject;
import org.xclcharts.chart.LineData;
import org.xclcharts.renderer.XEnum;

import java.net.MalformedURLException;
import java.security.acl.Group;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import android.graphics.Color;
import android.widget.Toast;
import android.os.Message;
import android.os.Handler;
import android.text.format.DateFormat;

import com.thfw.test.view.widget.StudyBendLine;
import com.thfw.test.view.widget.TouchProgessView;
import com.thfw.test.view.widget.AnalogController;

import static com.thfw.test.model.bean.RegionBean.prinfAllRegion;
import static com.thfw.test.view.mq.PahoHepler.LIGHT_OPRATE_ACTION;
import static com.thfw.test.view.mq.PahoHepler.LIGHT_OPRATE_ACTION_EXTRA_DATA;

/**
 * Created BY Cuishikuntv_label
 * Created ON 2021/11/10
 * Package Name com.thfw.test.view.activity
 **/
public class ControlActivity2 extends BaseActivity {
    public static int themeColor = Color.parseColor("#30CA6F");
    private static final String TAG = ControlActivity2.class.getCanonicalName();
    @BindView(R.id.rl_group_all)
    RelativeLayout rl_group_all;
    @BindView(R.id.rl_group1)
    RelativeLayout rl_group1;
    @BindView(R.id.rl_group2)
    RelativeLayout rl_group2;
    @BindView(R.id.rl_group3)
    RelativeLayout rl_group3;
    @BindView(R.id.rl_group_radom)
    RelativeLayout rl_group_radom;
    @BindView(R.id.tv_light1)
    TextView tv_light1;
    @BindView(R.id.tv_light2)
    TextView tv_light2;
    @BindView(R.id.tv_light3)
    TextView tv_light3;
    @BindView(R.id.tv_light4)
    TextView tv_light4;
    @BindView(R.id.tv_light5)
    TextView tv_light5;
    @BindView(R.id.tv_light6)
    TextView tv_light6;
    @BindView(R.id.tv_light7)
    TextView tv_light7;
    @BindView(R.id.tv_light8)
    TextView tv_light8;
    @BindView(R.id.tv_light9)
    TextView tv_light9;
    @BindView(R.id.iv_light1)
    ImageView iv_light1;
    @BindView(R.id.iv_light2)
    ImageView iv_light2;
    @BindView(R.id.iv_light3)
    ImageView iv_light3;
    @BindView(R.id.iv_light4)
    ImageView iv_light4;
    @BindView(R.id.iv_light5)
    ImageView iv_light5;
    @BindView(R.id.iv_light6)
    ImageView iv_light6;
    @BindView(R.id.iv_light7)
    ImageView iv_light7;
    @BindView(R.id.iv_light8)
    ImageView iv_light8;
    @BindView(R.id.iv_light9)
    ImageView iv_light9;
    @BindView(R.id.tv_group_all)
    TextView tv_group_all;
    @BindView(R.id.tv_group1)
    TextView tv_group1;
    @BindView(R.id.tv_group2)
    TextView tv_group2;
    @BindView(R.id.tv_group_radom)
    TextView tv_group_radom;
    @BindView(R.id.rl_aab)
    RelativeLayout rl_aab;
    @BindView(R.id.rl_aabb)
    RelativeLayout rl_aabb;
    @BindView(R.id.tv_group3)
    TextView tv_group3;
    @BindView(R.id.linechart1)
    StudyBendLine view1;
    @BindView(R.id.view_pg)
    TouchProgessView view_pg;
    @BindView(R.id.anlog1)
    AnalogController bassController;
    @BindView(R.id.anlog2)
    AnalogController bassController2;
    @BindView(R.id.iv_group_right)
    ImageView iv_group_right;
    @BindView(R.id.iv_group_left)
    ImageView iv_group_left;
    @BindView(R.id.iv_light_open)
    ImageView iv_light_open;
    @BindView(R.id.iv_jbsj_add)
    ImageView iv_jbsj_add;
    @BindView(R.id.iv_jbsj_delete)
    ImageView iv_jbsj_delete;
    @BindView(R.id.iv_cxsj_add)
    ImageView iv_cxsj_add;
    @BindView(R.id.iv_cxsj_delete)
    ImageView iv_cxsj_delete;
    @BindView(R.id.tv_jbsj_time)
    TextView tv_jbsj_time;
    @BindView(R.id.tv_cxsj_time)
    TextView tv_cxsj_time;
    @BindView(R.id.tv_kdsj_time_hour)
    TextView tv_kdsj_time_hour;
    @BindView(R.id.tv_kdsj_time_min)
    TextView tv_kdsj_time_min;
    @BindView(R.id.tv_gdsj_time_hour)
    TextView tv_gdsj_time_hour;
    @BindView(R.id.tv_gdsj_time_min)
    TextView tv_gdsj_time_min;
    @BindView(R.id.tv_yxsj)
    TextView tv_yxsj;

    @BindView(R.id.tv_jrnh_num)
    TextView tv_jrnh_num;
    @BindView(R.id.tv_jrnh_num2)
    TextView tv_jrnh_num2;
    @BindView(R.id.tv_jrnh_num3)
    TextView tv_jrnh_num3;


    @BindView(R.id.iv_kdsj_add)
    ImageView iv_kdsj_add;
    @BindView(R.id.iv_gdsj_add)
    ImageView iv_gdsj_add;
    @BindView(R.id.tv_control_time)
    TextView tv_control_time;
    @BindView(R.id.iv_control_power_off)
    ImageView iv_control_power_off;
    @BindView(R.id.tv_lable_all)
    TextView tv_lable_all;
    @BindView(R.id.cb_lighr1)
    CheckBox cb_lighr1;
    @BindView(R.id.cb_lighr2)
    CheckBox cb_lighr2;
    @BindView(R.id.cb_lighr3)
    CheckBox cb_lighr3;
    @BindView(R.id.cb_lighr4)
    CheckBox cb_lighr4;
    @BindView(R.id.cb_lighr5)
    CheckBox cb_lighr5;
    @BindView(R.id.cb_lighr6)
    CheckBox cb_lighr6;
    @BindView(R.id.cb_lighr7)
    CheckBox cb_lighr7;
    @BindView(R.id.cb_lighr8)
    CheckBox cb_lighr8;
    @BindView(R.id.cb_lighr9)
    CheckBox cb_lighr9;
    @BindView(R.id.gridview)
    GridView gridview;
    @BindView(R.id.lv)
    ListView lv;
    boolean isOpenGroupAll;
    boolean isOpenGroup1;
    boolean isOpenGroup2;
    boolean isOpenGroup3;
    boolean isOpenGroupRandom;

    boolean isOpenLight1;
    boolean isOpenLight2;
    boolean isOpenLight3;
    boolean isOpenLight4;
    boolean isOpenLight5;
    boolean isOpenLight6;
    boolean isOpenLight7;
    boolean isOpenLight8;
    boolean isOpenLight9;


    int group1Down = 1;
    int group1Up = 1;


    long lastTime;
    long lastWaysTime;
    long lastProgressTime;
    int lastProgressNum;

    class TimeThread extends Thread {
        @Override
        public void run() {
            do {
                try {
                    Thread.sleep(1000);
                    Message msg = new Message();
                    msg.what = 1;  //消息(一个整型值)
                    mHandler.sendMessage(msg);// 每隔1秒发送一个msg给mHandler
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } while (true);
        }
    }

    //在主线程里面处理消息并更新UI界面
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    long sysTime = System.currentTimeMillis();//获取系统时间
                    CharSequence sysTimeStr = DateFormat.format("yyyy年MM月dd日 hh:mm:ss", sysTime);//时间显示格式
                    tv_control_time.setText(sysTimeStr); //更新时间
                    break;
                default:
                    break;

            }
        }
    };

    @Override
    public int getLayoutId() {
        return R.layout.activity_control;
    }

    ArrayList<TextView> textViewList;
    ArrayList<CheckBox> checkBoxlist;
    ArrayList<ImageView> imageViewList;

    @Override
    public void initView() {
        //初始化分组
        textViewList = new ArrayList<>();
        checkBoxlist = new ArrayList<>();
        imageViewList = new ArrayList<>();
        textViewList.add(tv_light1);
        textViewList.add(tv_light2);
        textViewList.add(tv_light3);
        textViewList.add(tv_light4);
        textViewList.add(tv_light5);
        textViewList.add(tv_light6);
        textViewList.add(tv_light7);
        textViewList.add(tv_light8);
        textViewList.add(tv_light9);
        checkBoxlist.add(cb_lighr1);
        checkBoxlist.add(cb_lighr2);
        checkBoxlist.add(cb_lighr3);
        checkBoxlist.add(cb_lighr4);
        checkBoxlist.add(cb_lighr5);
        checkBoxlist.add(cb_lighr6);
        checkBoxlist.add(cb_lighr7);
        checkBoxlist.add(cb_lighr8);
        checkBoxlist.add(cb_lighr9);
        imageViewList.add(iv_light1);
        imageViewList.add(iv_light2);
        imageViewList.add(iv_light3);
        imageViewList.add(iv_light4);
        imageViewList.add(iv_light5);
        imageViewList.add(iv_light6);
        imageViewList.add(iv_light7);
        imageViewList.add(iv_light8);
        imageViewList.add(iv_light9);
        new TimeThread().start(); //启动新的线程
        //获取所有灯具信息
        getLampList();
        //获取分组信息
        getGroupData();
        //获取区域信息
        getRegion();
        //显示全部能耗信息
        getEnergyStatistics("0", true);

        //绑定广播监听事件
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(LIGHT_OPRATE_ACTION);
        registerReceiver(my, intentFilter);

        tv_kdsj_time_hour.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (tv_kdsj_time_hour.isSelected()) {
                    tv_kdsj_time_hour.setTextColor(getResources().getColor(R.color.wmd_text_color_green));
                    tv_kdsj_time_hour.setSelected(false);
                } else {
                    tv_kdsj_time_hour.setTextColor(getResources().getColor(R.color.red));
                    tv_kdsj_time_hour.setSelected(true);
                    tv_kdsj_time_min.setSelected(false);
                    tv_kdsj_time_min.setTextColor(getResources().getColor(R.color.wmd_text_color_green));
                }
            }
        });


        tv_kdsj_time_min.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (tv_kdsj_time_min.isSelected()) {
                    tv_kdsj_time_min.setTextColor(getResources().getColor(R.color.wmd_text_color_green));
                    tv_kdsj_time_min.setSelected(false);
                } else {
                    tv_kdsj_time_min.setTextColor(getResources().getColor(R.color.red));
                    tv_kdsj_time_min.setSelected(true);
                    tv_kdsj_time_hour.setSelected(false);
                    tv_kdsj_time_hour.setTextColor(getResources().getColor(R.color.wmd_text_color_green));
                }
            }
        });

        tv_gdsj_time_hour.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (tv_gdsj_time_hour.isSelected()) {
                    tv_gdsj_time_hour.setTextColor(getResources().getColor(R.color.wmd_text_color_green));
                    tv_gdsj_time_hour.setSelected(false);
                } else {
                    tv_gdsj_time_hour.setTextColor(getResources().getColor(R.color.red));
                    tv_gdsj_time_hour.setSelected(true);
                    tv_gdsj_time_min.setSelected(false);
                    tv_gdsj_time_min.setTextColor(getResources().getColor(R.color.wmd_text_color_green));
                }
            }
        });


        tv_gdsj_time_min.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (tv_gdsj_time_min.isSelected()) {
                    tv_gdsj_time_min.setTextColor(getResources().getColor(R.color.wmd_text_color_green));
                    tv_gdsj_time_min.setSelected(false);
                } else {
                    tv_gdsj_time_min.setTextColor(getResources().getColor(R.color.red));
                    tv_gdsj_time_min.setSelected(true);
                    tv_gdsj_time_hour.setSelected(false);
                    tv_gdsj_time_hour.setTextColor(getResources().getColor(R.color.wmd_text_color_green));
                }
            }
        });


        rl_aab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                showTimePick1(tv_kdsj_time);
                if (getGroupAdapterSuccess) {
                    if (tv_kdsj_time_hour.isSelected()) {
                        int hour = (Integer.valueOf(tv_kdsj_time_hour.getText().toString())) + 1;
                        if (hour < 10) {
                            tv_kdsj_time_hour.setText("0" + hour + "");
                        } else {
                            tv_kdsj_time_hour.setText(hour + "");
                        }
                        if (hour == 24) {
                            tv_kdsj_time_hour.setText("00");
                        }
                        lastWaysTime = System.currentTimeMillis();
//                    saveTime();
                        refreashWays();
                        GroupAdapter.Light light = map.get(groupAdapter.getSelectedGroupId());
                        light.setGroup1TimeOpenHour(tv_kdsj_time_hour.getText().toString());
                        map.put(groupAdapter.getSelectedGroupId(), light);
                    }
                    if (tv_kdsj_time_min.isSelected()) {
                        int hour = (Integer.valueOf(tv_kdsj_time_min.getText().toString())) + 15;
                        if (hour < 10) {
                            tv_kdsj_time_min.setText("0" + hour + "");
                        } else {
                            tv_kdsj_time_min.setText(hour + "");
                        }
                        if (hour == 60) {
                            tv_kdsj_time_min.setText("00");
                        }
                        lastWaysTime = System.currentTimeMillis();
//                    saveTime();
                        GroupAdapter.Light light = map.get(groupAdapter.getSelectedGroupId());
                        light.setGroup1TimeOpenMin(tv_kdsj_time_min.getText().toString());
                        map.put(groupAdapter.getSelectedGroupId(), light);
                        refreashWays();
                    }

                }
            }
        });


        rl_aabb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getGroupAdapterSuccess) {
                    if (tv_gdsj_time_hour.isSelected()) {
                        int hour = (Integer.valueOf(tv_gdsj_time_hour.getText().toString())) + 1;
                        if (hour < 10) {
                            tv_gdsj_time_hour.setText("0" + hour + "");
                        } else {
                            tv_gdsj_time_hour.setText(hour + "");
                        }
//                    tv_gdsj_time_hour.setText(hour + "");
                        if (hour == 24) {
                            tv_gdsj_time_hour.setText("00");
                        }
                        lastWaysTime = System.currentTimeMillis();
                        GroupAdapter.Light light = map.get(groupAdapter.getSelectedGroupId());
                        light.setGroup1TimeCloseHour(tv_gdsj_time_hour.getText().toString());
                        map.put(groupAdapter.getSelectedGroupId(), light);
                        refreashWays();
                    }
                    if (tv_gdsj_time_min.isSelected()) {
                        int hour = (Integer.valueOf(tv_gdsj_time_min.getText().toString())) + 15;
                        if (hour < 10) {
                            tv_gdsj_time_min.setText("0" + hour + "");
                        } else {
                            tv_gdsj_time_min.setText(hour + "");
                        }

                        if (hour == 60) {
                            tv_gdsj_time_min.setText("00");
                        }
                        lastWaysTime = System.currentTimeMillis();
                        GroupAdapter.Light light = map.get(groupAdapter.getSelectedGroupId());
                        light.setGroup1TimeCloseMin(tv_gdsj_time_min.getText().toString());
                        map.put(groupAdapter.getSelectedGroupId(), light);
                        refreashWays();
                    }
                }
            }
        });

        iv_group_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getGroupAdapterSuccess) {
                    int num = groupAdapter.getCount();
                    if (nowGroupPosition == -1) {
                        nowGroupPosition = 0;
                        groupAdapter.setIconSelected(groupList.get(0).getGroupId());
                    } else if (nowGroupPosition == groupAdapter.getCount() - 1) {
                        nowGroupPosition = 0;
                        groupAdapter.setIconSelected(groupList.get(0).getGroupId());
                    } else if (nowGroupPosition < groupAdapter.getCount() - 1) {
                        nowGroupPosition = nowGroupPosition + 1;
                        groupAdapter.setIconSelected(groupList.get(nowGroupPosition).getGroupId());
                    }
                    int groupId = Integer.valueOf(groupAdapter.getSelectedGroupId());
                    if (groupId == -1) {
                        lightAdapter.setAllCheckGroupData(true);
                        getEnergyStatistics("0", true);
                    } else if (groupId == -2) {
                        lightAdapter.setAllCheckGroupData(false);
                        getEnergyStatistics("0", true);
                    } else {
                        lightAdapter.setCheckGroupData(groupId);
                        getEnergyStatistics(groupList.get(nowGroupPosition).getGroupId(), false);
                    }
                }
            }
        });
        //灯光打开调用
        iv_light_open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getGroupAdapterSuccess) {
                    if (System.currentTimeMillis() - lastTime > 2000) {
                        if (iv_light_open.isSelected()) {
                            //全局灯光关闭
                            if (groupAdapter.getSelectedGroupId().equals("-1")) {
                                ArrayList<LampListBean> list = new ArrayList<>();
                                for (LampListBean bean : lampList) {
                                    bean.setIsOpen("0");
                                    list.add(bean);
                                }
                                openOrCloseConbinationGroup(list, "全局灯光关闭", false, true);
                            }
                            //任意灯光组关闭
                            else if (groupAdapter.getSelectedGroupId().equals("-2")) {
                                ArrayList<LampListBean> list = new ArrayList<>();
                                for (LampListBean bean : checkBoxLampList) {
                                    bean.setIsOpen("0");
                                    list.add(bean);
                                }
                                openOrCloseConbinationGroup(checkBoxLampList, "任意组灯光关闭", false, false);
                            } else {
                                //组级别灯光关闭
                                lightOpenAll(0, groupAdapter.getSelectedGroupId(), "分组灯光关闭", 2);
                            }


                        } else {

                            //全局灯光打开
                            if (groupAdapter.getSelectedGroupId().equals("-1")) {
                                ArrayList<LampListBean> list = new ArrayList<>();
                                for (LampListBean bean : lampList) {
                                    bean.setIsOpen("1");
                                    list.add(bean);
                                }
                                openOrCloseConbinationGroup(list, "全局灯光打开", true, true);
                            }
                            //任意灯光组打开
                            else if (groupAdapter.getSelectedGroupId().equals("-2")) {
                                ArrayList<LampListBean> list = new ArrayList<>();
                                for (LampListBean bean : checkBoxLampList) {
                                    bean.setIsOpen("1");
                                    list.add(bean);
                                }
                                openOrCloseConbinationGroup(checkBoxLampList, "任意组灯光打开", true, false);
                            } else {
                                //组级别灯光打开
                                lightOpenAll(1, groupAdapter.getSelectedGroupId(), "分组灯光打开", 2);
                            }


                        }
//                        checkBoxLampList.clear();
                    } else {
                        ToastUtils.showToast(ControlActivity2.this, "请勿频繁执行开关灯操作");

                    }
                }

            }
        });

        iv_group_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getGroupAdapterSuccess) {
                    int num = groupAdapter.getCount();
                    if (nowGroupPosition == -1) {
                        nowGroupPosition = 0;
                        groupAdapter.setIconSelected(groupList.get(0).getGroupId());
                    } else if (nowGroupPosition == 0) {
                        groupAdapter.setIconSelected(groupList.get(num - 1).getGroupId());
                        nowGroupPosition = num - 1;
                    } else if (nowGroupPosition == groupAdapter.getCount()) {
                        nowGroupPosition = nowGroupPosition - 1;
                        groupAdapter.setIconSelected(groupList.get(nowGroupPosition - 1).getGroupId());
                    } else if (nowGroupPosition < groupAdapter.getCount()) {
                        nowGroupPosition = nowGroupPosition - 1;
                        groupAdapter.setIconSelected(groupList.get(nowGroupPosition).getGroupId());
                    }
                    int groupId = Integer.valueOf(groupAdapter.getSelectedGroupId());
                    if (groupId == -1) {
                        lightAdapter.setAllCheckGroupData(true);
                        getEnergyStatistics("0", true);
                    } else if (groupId == -2) {
                        lightAdapter.setAllCheckGroupData(false);
                        getEnergyStatistics("0", true);
                    } else {
                        lightAdapter.setCheckGroupData(groupId);
                        getEnergyStatistics(groupList.get(nowGroupPosition).getGroupId(), false);
                    }

                }
            }
        });

        //持续时间
        iv_jbsj_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getGroupAdapterSuccess) {
                    int num = Integer.valueOf(tv_jbsj_time.getText().toString()) + 1;
                    if (num == 6) {
                        num = 5;
                        tv_jbsj_time.setText(num + "");
                        ToastUtil.showToast(ControlActivity2.this, "已到达上限");

                    } else {
                        lastWaysTime = System.currentTimeMillis();
                        refreashWays();
                        tv_jbsj_time.setText(num + "");
                    }

                    GroupAdapter.Light light = map.get(groupAdapter.getSelectedGroupId());
                    light.setJbsjGroup(num);
                    map.put(groupAdapter.getSelectedGroupId(), light);
                }

            }
        });

        iv_jbsj_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getGroupAdapterSuccess) {
                    int num = Integer.valueOf(tv_jbsj_time.getText().toString()) - 1;
                    if (num == 0) {
                        num = 1;
                        ToastUtil.showToast(ControlActivity2.this, "已到达下限");
                        if (num > 0) {
                            tv_jbsj_time.setText(num + "");
                        }

                    } else {
                        lastWaysTime = System.currentTimeMillis();
                        refreashWays();
                        if (num > 0) {
                            tv_jbsj_time.setText(num + "");
                        }
                    }
                    //记录不同组别渐变时间
                    GroupAdapter.Light light = map.get(groupAdapter.getSelectedGroupId());
                    light.setJbsjGroup(num);
                    map.put(groupAdapter.getSelectedGroupId(), light);
                }

            }
        });

        //持续时间
        iv_cxsj_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getGroupAdapterSuccess) {
                    int num = Integer.valueOf(tv_cxsj_time.getText().toString()) + 1;
                    if (num == 31) {
                        num = 30;
                        ToastUtil.showToast(ControlActivity2.this, "已到达上限");

                        tv_cxsj_time.setText(num + "");

                    } else {
                        lastWaysTime = System.currentTimeMillis();
                        refreashWays();

                        tv_cxsj_time.setText(num + "");

                    }

                    GroupAdapter.Light light = map.get(groupAdapter.getSelectedGroupId());
                    light.setCxsjGroup(num);
                    map.put(groupAdapter.getSelectedGroupId(), light);
                }
            }
        });

        iv_cxsj_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getGroupAdapterSuccess) {
                    int num = Integer.valueOf(tv_cxsj_time.getText().toString()) - 1;

                    if (num == 4) {
                        num = 5;
                        ToastUtil.showToast(ControlActivity2.this, "已到达下限");
                        if (num > 0) {
                            tv_cxsj_time.setText(num + "");
                        }
                    } else {
                        lastWaysTime = System.currentTimeMillis();
                        refreashWays();
                        if (num > 0) {
                            tv_cxsj_time.setText(num + "");
                        }
                    }

                    GroupAdapter.Light light = map.get(groupAdapter.getSelectedGroupId());
                    light.setCxsjGroup(num);
                    map.put(groupAdapter.getSelectedGroupId(), light);

                }
            }
        });

        view_pg.setProgress(5);
        view_pg.setOnProgressChangedListener(new TouchProgessView.OnProgressChangedListener() {
            @Override
            public void onProgressChanged(View view, int progress) {
                if (progress < 5) {
                    view_pg.setProgress(5);
                    Log.d("TouchProgessView", "view_pg.setProgress(5)");
//                    this.onProgressSuccess(view,progress);
                }
            }

            @Override
            public void onProgressSuccess(View view, int progress) {
                if (getGroupAdapterSuccess) {
                    int num = 0;
                    if (System.currentTimeMillis() - lastProgressTime > 1000) {

                        num = progress;
                        if (progress < 5) {
                            view_pg.setProgress(5);
                            num = 5;
                        }
                        if (progress > 100) {
                            view_pg.setProgress(100);
                            num = 100;
                        }

                        int groupId = Integer.valueOf(groupAdapter.getSelectedGroupId());
                        Log.d("TouchProgessView", "onProgressSuccess = " + num);
                        if (groupId == -1) {
                            ArrayList<LampListBean> list = new ArrayList<>();
                            for (LampListBean bean : lampList) {
                                bean.setLight(num + "");
                                list.add(bean);
                            }
                            brightnessConbinationGroup(list);
                        } else if (groupId == -2) {
                            Log.d("TouchProgessView", "自定义分组灯光调节");

                            ArrayList<LampListBean> list = new ArrayList<>();
                            for (LampListBean bean : checkBoxLampList) {
                                bean.setLight(num + "");
                                list.add(bean);
                            }
                            brightnessConbinationGroup(list);
                        } else {
                            brightnessAll(num, String.valueOf(groupId));
                        }

                        lastProgressNum = num;
                        //保存每页亮度
                        GroupAdapter.Light light = map.get(groupAdapter.getSelectedGroupId());
                        light.setGroupProgress(lastProgressNum);
                        map.put(groupAdapter.getSelectedGroupId(), light);

                    } else {
                        ToastUtil.showToast(ControlActivity2.this, "亮度调节频繁，请稍后再试");
                        view_pg.setProgress(lastProgressNum);
                    }

//                Toast.makeText(ControlActivity2.this, "当前亮度已调节为" + progress, Toast.LENGTH_SHORT).show();
                    lastProgressTime = System.currentTimeMillis();

                }
            }
        });


        bassController.circlePaint2.setColor(themeColor);
        bassController.linePaint.setColor(themeColor);
        bassController.invalidate();
        bassController.linePaint.setColor(themeColor);
        bassController.setProgress(1);


        bassController2.circlePaint2.setColor(themeColor);
        bassController2.linePaint.setColor(themeColor);
        bassController2.invalidate();
        bassController2.linePaint.setColor(themeColor);
        bassController2.setProgress(1);

        bassController2.setOnProgressChangedListener(new AnalogController.onProgressChangedListener() {
            @Override
            public void onProgressChanged(int progress) {
                if (getGroupAdapterSuccess) {
                    int num;

                    group1Down = Integer.valueOf(bassController2.getAngel());
                    if (group1Up < group1Down) {
                        group1Up = Integer.valueOf(bassController2.getAngel());
                        bassController.setProgress(group1Up);
                    }

                    GroupAdapter.Light light = map.get(groupAdapter.getSelectedGroupId());
                    light.setGroupUp(group1Up);
                    map.put(groupAdapter.getSelectedGroupId(), light);


                    lastWaysTime = System.currentTimeMillis();
                    refreashWays();
                }
            }
        });


        bassController.setOnProgressChangedListener(new AnalogController.onProgressChangedListener() {
            @Override
            public void onProgressChanged(int progress) {
                if (getGroupAdapterSuccess) {
                    group1Up = Integer.valueOf(bassController.getAngel());
                    if (group1Down > group1Up) {
                        bassController.setProgress(group1Down);
                    }
                    GroupAdapter.Light light = map.get(groupAdapter.getSelectedGroupId());
                    light.setGroupDown(group1Down);
                    map.put(groupAdapter.getSelectedGroupId(), light);
                    lastWaysTime = System.currentTimeMillis();
                    refreashWays();
                }
            }
        });
        iv_control_power_off.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(ControlActivity2.this);
                //给对话框添加内容
                builder.setMessage("是否确定退出登陆");
                //给对话框添加点击事件

                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
//                        Log.e("对话框", "取消 which = " + which);
                    }
                });

                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ToastUtil.showToast(ControlActivity2.this, "登出成功");
                        AppActivities.instance().finishTo(ControlActivity2.this, LoginActivity.class);
                    }
                });
                //切记勿忘~开启dialog
                builder.show();
            }
        });

        ArrayList<Integer> timeList = new ArrayList<>();
        timeList.add(1000);
        timeList.add(1500);
        timeList.add(1200);
        timeList.add(2200);
        timeList.add(2600);
        timeList.add(1900);
        timeList.add(4000);
        timeList.add(1200);
        ArrayList<String> bottomList = new ArrayList<>();
        bottomList.add("1.10");
        bottomList.add("1.11");
        bottomList.add("1.12");
        bottomList.add("1.13");
        bottomList.add("1.14");
        bottomList.add("1.15");
        bottomList.add("1.16");
        bottomList.add("1.17");
        view1.updateTime(timeList, bottomList);
    }


    int port;
    String host;
    ArrayList<GroupBean> groupList;
    private boolean getGroupAdapterSuccess;

    public String getGroupId(int state) {
        if (groupList != null && groupList.size() > state) {
            return groupList.get(state).getGroupId();
        } else {
            return "";
        }
    }


    private int nowGroupPosition = -1;
    private Map<String, GroupAdapter.Light> map;

    //获取灯具分组
    private void getGroupData() {
        try {

            String url = StringUtils.buildURL("http", host, port, "", null).toString();
            String token = getConfig().getString(ConfigurationEntry.TOKEN, "");
            Map<String, String> params = new HashMap<>();
//            params.put("regionId", nowRegion.getValue());
            RetrofitWrapper.getInstance(ControlActivity2.this, url).create(AuthManageService.class).getLightGroupNew(token, params).enqueue(new Callback<AuthBaseBean<ArrayList<GroupBean>>>() {
                @Override
                public void onResponse(Call<AuthBaseBean<ArrayList<GroupBean>>> call, Response<AuthBaseBean<ArrayList<GroupBean>>> response) {
                    SCLog.e(TAG, "response is onResponse" + call.toString());
                    if (response.body() != null && response.body().getData() != null) {
                        groupList = new ArrayList<>();
                        GroupBean bean1 = new GroupBean();
                        bean1.setGroupId("-1");
                        bean1.setGroupName("全局");
                        groupList.add(bean1);
                        groupList.addAll(response.body().getData());
                        ToastUtil.showToast(ControlActivity2.this, "灯具分组信息获取成功");
                        GroupBean bean = new GroupBean();
                        bean.setGroupId("-2");
                        bean.setGroupName("任意");
                        groupList.add(bean);
                        map = new HashMap<>();
                        for (GroupBean bean2 : groupList) {
                            GroupAdapter.Light light = new GroupAdapter.Light();
                            map.put(bean2.getGroupId(), light);
                        }
                        getGroupAdapterSuccess = true;
                        groupAdapter = new GroupAdapter(getApplicationContext(), groupList, map);
                        lv.setAdapter(groupAdapter);
                        groupAdapter.setOnClickMyTextView(new OnItemCallBack() {
                            @Override
                            public void onSelectedCallBack(int pos, boolean remove) {
                                nowGroupPosition = pos;
                                if (lightAdapter.getLightState()) {
                                    iv_light_open.setSelected(true);
                                } else {
                                    iv_light_open.setSelected(false);
                                }

                                int groupId = Integer.valueOf(groupList.get(pos).getGroupId());
                                if (groupId == -1) {
                                    lightAdapter.setAllCheckGroupData(true);
                                    getEnergyStatistics("0", true);
                                } else if (groupId == -2) {
                                    lightAdapter.setAllCheckGroupData(false);
                                    getEnergyStatistics("0", true);
                                } else {
                                    lightAdapter.setCheckGroupData(groupId);
                                    getEnergyStatistics(groupList.get(pos).getGroupId(), false);

                                }
                                groupAdapter.setIconSelected(groupList.get(pos).getGroupId());
                                GroupAdapter.Light light = map.get(groupAdapter.getSelectedGroupId());
                                view_pg.setProgress(light.getGroupProgress());
                                tv_jbsj_time.setText(light.getJbsjGroup() + "");
                                tv_cxsj_time.setText(light.getCxsjGroup() + "");
                                bassController.setProgress(light.getGroupDown());
                                bassController2.setProgress(light.getGroupUp());
                                tv_kdsj_time_hour.setText(light.getGroup1TimeOpenHour());
                                tv_kdsj_time_min.setText(light.getGroup1TimeOpenMin());
                                tv_gdsj_time_hour.setText(light.getGroup1TimeCloseHour());
                                tv_gdsj_time_min.setText(light.getGroup1TimeCloseMin());
                                checkBoxLampList.clear();
                            }

                            @Override
                            public void onSelectedDataCallBack(GroupAdapter.Light bean) {

                            }
                        });
                    }

                }

                @Override
                public void onFailure(Call<AuthBaseBean<ArrayList<GroupBean>>> call, Throwable t) {
                    SCLog.e(TAG, "灯组信息获取失败" + call.toString() + t.getMessage());

                }
            });
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    //控制灯光打开关闭
    private void lightOpenAll(int isOpen, String groupId, String message, int groupNum) {
        try {
            String url = StringUtils.buildURL("http", host, port, "", null).toString();
            String token = getConfig().getString(ConfigurationEntry.TOKEN, "");
            Map<String, String> params = new HashMap<>();
            params.put("groupId", groupId);
            params.put("isOpen", isOpen + "");

            RequestBody requestBody = RequestBody.create(MediaType.parse("Content-Type, application/json;"),
                    new JSONObject(params).toString());

            RetrofitWrapper.getInstance(ControlActivity2.this, url).create(AuthManageService.class).lightOpenAll(token, requestBody).enqueue(new Callback<AuthBaseBean<Object>>() {
                @Override
                public void onResponse(Call<AuthBaseBean<Object>> call, Response<AuthBaseBean<Object>> response) {
                    SCLog.e(TAG, "response is onResponse" + call.toString());
                    Toast.makeText(ControlActivity2.this, message, Toast.LENGTH_SHORT).show();

                    if (isOpen == 1) {
                        lightAdapter.setGroupLightSmart(Integer.valueOf(groupAdapter.getSelectedGroupId()), true, false, checkBoxLampList);
                        iv_light_open.setSelected(true);
                    } else {
                        lightAdapter.setGroupLightSmart(Integer.valueOf(groupAdapter.getSelectedGroupId()), false, false, checkBoxLampList);
                        iv_light_open.setSelected(false);
                    }

                }

                @Override
                public void onFailure(Call<AuthBaseBean<Object>> call, Throwable t) {
                    SCLog.e(TAG, "response is onFailure" + call.toString());
                    Toast.makeText(ControlActivity2.this, "组灯光操作失败" + t.getMessage(), Toast.LENGTH_SHORT).show();

                }
            });
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }


    //控制灯光亮度
    private void brightnessAll(int level, String groupId) {
        try {
            String url = StringUtils.buildURL("http", host, port, "", null).toString();
            String token = getConfig().getString(ConfigurationEntry.TOKEN, "");
            Map<String, String> params = new HashMap<String, String>();
            params.put("groupId", groupId);
            params.put("light", level + "");

            RequestBody requestBody = RequestBody.create(MediaType.parse("Content-Type, application/json;"),
                    new JSONObject(params).toString());

            RetrofitWrapper.getInstance(ControlActivity2.this, url).create(AuthManageService.class).brightnessAll(token, requestBody).enqueue(new Callback<AuthBaseBean<Object>>() {
                @Override
                public void onResponse(Call<AuthBaseBean<Object>> call, Response<AuthBaseBean<Object>> response) {
                    SCLog.e(TAG, "response is onResponse" + call.toString());
                    Toast.makeText(ControlActivity2.this, "第" + groupId + "组灯光亮度调节" + level + "成功", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onFailure(Call<AuthBaseBean<Object>> call, Throwable t) {
                    SCLog.e(TAG, "response is onFailure" + call.toString());
                    Toast.makeText(ControlActivity2.this, "第" + groupId + "组灯光亮度调节" + level + "失败" + t.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    //设置生效策略

    private void add4TimeAndMan(String groupId) {
        try {
            String url = StringUtils.buildURL("http", host, port, "", null).toString();
            String token = getConfig().getString(ConfigurationEntry.TOKEN, "");
            BindingBean parms = new BindingBean();
            parms.setBindingState(1);
            parms.setManState(1);
            if (rl_group_all.isSelected()) {
                parms.setRegionId(nowRegion.getValue());
                parms.setType(0);
            } else {
                parms.setType(1);
                parms.setGrpId(groupId);
            }


            parms.setMan(getMan());
            parms.setTime(getTime());
            RequestBody requestBody = RequestBody.create(MediaType.parse("Content-Type, application/json;"),
                    new Gson().toJson(parms));

            RetrofitWrapper.getInstance(ControlActivity2.this, url).create(AuthManageService.class).add4TimeAndMan(token, requestBody).enqueue(new Callback<AuthBaseBean<BindingBean>>() {
                @Override
                public void onResponse(Call<AuthBaseBean<BindingBean>> call, Response<AuthBaseBean<BindingBean>> response) {
                    SCLog.e(TAG, "response is onResponse" + call.toString());
//                    Toast.makeText(ControlActivity2.this, "第" + groupId + "策略生效成功", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onFailure(Call<AuthBaseBean<BindingBean>> call, Throwable t) {
                    SCLog.e(TAG, "response is onFailure" + call.toString());
                    Toast.makeText(ControlActivity2.this, "第" + groupId + "策略生效失败" + t.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    //修改接口
    private void editAdd4TimeAndMan(String groupId) {
        try {
            String url = StringUtils.buildURL("http", host, port, "", null).toString();
            String token = getConfig().getString(ConfigurationEntry.TOKEN, "");
            BindingBean parms = new BindingBean();
            parms.setBindingState(1);
            parms.setManState(1);
            if (groupAdapter.getSelectedGroupId().equals("-1")) {
                parms.setRegionId(nowRegion.getValue());
                parms.setType(0);
            } else {
                parms.setType(1);
                parms.setGrpId(groupId);
            }


            parms.setMan(getMan());
            parms.setTime(getTime());
            RequestBody requestBody = RequestBody.create(MediaType.parse("Content-Type, application/json;"),
                    new Gson().toJson(parms));

            RetrofitWrapper.getInstance(ControlActivity2.this, url).create(AuthManageService.class).editAdd4TimeAndMan(token, requestBody).enqueue(new Callback<AuthBaseBean<BindingBean>>() {
                @Override
                public void onResponse(Call<AuthBaseBean<BindingBean>> call, Response<AuthBaseBean<BindingBean>> response) {
                    SCLog.e(TAG, "response is onResponse" + call.toString());
//                    Toast.makeText(ControlActivity2.this, "第" + groupId + "策略生效成功", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onFailure(Call<AuthBaseBean<BindingBean>> call, Throwable t) {
                    SCLog.e(TAG, "response is onFailure" + call.toString());
                    Toast.makeText(ControlActivity2.this, "第" + groupId + "策略生效失败" + t.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }


    //任意组 控灯亮度
    private void brightnessConbinationGroup(ArrayList<LampListBean> list) {
        try {
            String url = StringUtils.buildURL("http", host, port, "", null).toString();
            String token = getConfig().getString(ConfigurationEntry.TOKEN, "");

            Gson gson = new Gson();
            String str = gson.toJson(list);

            RequestBody requestBody = RequestBody.create(MediaType.parse("Content-Type, application/json;"),
                    str);

            RetrofitWrapper.getInstance(ControlActivity2.this, url).create(AuthManageService.class).brightnessConbinationGroup(token, requestBody).enqueue(new Callback<AuthBaseBean<Object>>() {
                @Override
                public void onResponse(Call<AuthBaseBean<Object>> call, Response<AuthBaseBean<Object>> response) {
                    SCLog.e(TAG, "response is onResponse" + call.toString());
                    Toast.makeText(ControlActivity2.this, "任意组亮度调节成功", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onFailure(Call<AuthBaseBean<Object>> call, Throwable t) {
                    SCLog.e(TAG, "response is onFailure" + call.toString());
                    Toast.makeText(ControlActivity2.this, "任意组亮度调节失败" + t.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }
    //获取首页节能信息

    //控制灯光亮度
    private void getEnergyStatistics(String groupId, boolean isAll) {
        try {
            String url = StringUtils.buildURL("http", host, port, "", null).toString();
            String token = getConfig().getString(ConfigurationEntry.TOKEN, "");
            Map<String, String> params = new HashMap<String, String>();
            if (isAll) {
                params.put("groupId", null);
            } else {
                params.put("groupId", groupId);
            }
            RequestBody requestBody = RequestBody.create(MediaType.parse("Content-Type, application/json;"),
                    new JSONObject(params).toString());

            RetrofitWrapper.getInstance(ControlActivity2.this, url).create(AuthManageService.class).getEnergyStatistics(token, requestBody).enqueue(new Callback<AuthBaseBean<MainEnergyBean>>() {
                @Override
                public void onResponse(Call<AuthBaseBean<MainEnergyBean>> call, Response<AuthBaseBean<MainEnergyBean>> response) {
                    if (response.body() != null && response.body().getData() != null) {
                        tv_jrnh_num.setText(response.body().getData().getToday().getEnergy());
                        tv_jrnh_num2.setText(response.body().getData().getTodaySave().getSaveEnergy());
                        tv_jrnh_num3.setText(response.body().getData().getAllDaySave().getSaveEnergy());
                        tv_yxsj.setText("系统累计运行时长：" + response.body().getData().getEarliestInstallLampTime());
                    }
                }

                @Override
                public void onFailure(Call<AuthBaseBean<MainEnergyBean>> call, Throwable t) {
                    SCLog.e(TAG, "response is onFailure" + call.toString());
                    Toast.makeText(ControlActivity2.this, "操作失败", Toast.LENGTH_SHORT).show();
                }
            });
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }


    //任意组 控灯开关
    private void openOrCloseConbinationGroup(ArrayList<LampListBean> list, String message, boolean isOpen, boolean isAll) {
        try {
            String url = StringUtils.buildURL("http", host, port, "", null).toString();
            String token = getConfig().getString(ConfigurationEntry.TOKEN, "");

            Gson gson = new Gson();
            String str = gson.toJson(list);

            RequestBody requestBody = RequestBody.create(MediaType.parse("Content-Type, application/json;"),
                    str);

            RetrofitWrapper.getInstance(ControlActivity2.this, url).create(AuthManageService.class).openOrClose(token, requestBody).enqueue(new Callback<AuthBaseBean<Object>>() {
                @Override
                public void onResponse(Call<AuthBaseBean<Object>> call, Response<AuthBaseBean<Object>> response) {
                    SCLog.e(TAG, "response is onResponse" + call.toString());
                    Toast.makeText(ControlActivity2.this, message, Toast.LENGTH_SHORT).show();
                    if (isOpen) {
                        iv_light_open.setSelected(true);
                        if(isAll){
                            PahoHepler.getInstance().pushLightOprate("region",Integer.valueOf(nowRegion.getValue()),"1");
                        }
                    } else {
                        iv_light_open.setSelected(false);
                        if(isAll){
                            PahoHepler.getInstance().pushLightOprate("region",Integer.valueOf(nowRegion.getValue()),"0");
                        }
                    }


                    getLampList();
                }

                @Override
                public void onFailure(Call<AuthBaseBean<Object>> call, Throwable t) {
                    SCLog.e(TAG, "response is onFailure" + call.toString());
                    Toast.makeText(ControlActivity2.this, "操作失败" + t.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }


    //获取策略
    private BindingBean.Man getMan() {
        BindingBean.Man man = new BindingBean.Man();
        man.setDelayTime(Integer.valueOf(tv_cxsj_time.getText().toString()));
        man.setChangeNot(Integer.valueOf(tv_jbsj_time.getText().toString()));
        man.setMaxManLight(bassController.getAngel());
        man.setMinManLight(bassController2.getAngel());
        return man;
    }


    private BindingBean.Time getTime() {
        BindingBean.Time man = new BindingBean.Time();

        man.setOpenTime(tv_kdsj_time_hour.getText().toString() + ":" + tv_kdsj_time_min.getText().toString());
        man.setShutTime(tv_gdsj_time_hour.getText().toString() + ":" + tv_gdsj_time_min.getText().toString());
//        man.setLight(view_pg.getProgressNum());
        man.setJobType(1);
        return man;
    }

    private ArrayList<LampListBean> lampList = new ArrayList<>();
    LightAdapter lightAdapter;
    GroupAdapter groupAdapter;
    ArrayList<LampListBean> checkBoxLampList = new ArrayList<>();

    //获取所有灯具信息
    private void getLampList() {
        try {
            String url = StringUtils.buildURL("http", host, port, "", null).toString();
            String token = getConfig().getString(ConfigurationEntry.TOKEN, "");

            RetrofitWrapper.getInstance(ControlActivity2.this, url).create(AuthManageService.class).getLampList(token).enqueue(new Callback<LampInterfaceBean<ArrayList<LampListBean>>>() {
                @Override
                public void onResponse(Call<LampInterfaceBean<ArrayList<LampListBean>>> call, Response<LampInterfaceBean<ArrayList<LampListBean>>> response) {
                    SCLog.e(TAG, "response is onResponse" + call.toString());
                    if (response != null && response.body() != null && response.body().getRows() != null) {
                        ArrayList<LampListBean> list = response.body().getRows();
//                        refreashLight();
                        lampList = new ArrayList<>();
                        for (int i = 0; i < list.size(); i++) {
                            LampListBean bean = list.get(i);
                            bean.setSerialNum(i + 1 + "");
                            lampList.add(bean);
                        }
                        if (lightAdapter == null) {
                            lightAdapter = new LightAdapter(getApplicationContext(), lampList);
                            gridview.setAdapter(lightAdapter);
                            lightAdapter.setOnClickMyTextView(new OnItemCallBack() {
                                @Override
                                public void onSelectedCallBack(int pos, boolean remve) {
                                    if (getGroupAdapterSuccess) {
                                        if (groupAdapter.getSelectedGroupId().equals("-2")) {
                                            if (remve) {
                                                if (checkBoxLampList.contains(lampList.get(pos))) {
                                                    checkBoxLampList.remove(lampList.get(pos));
                                                }
                                            } else {
                                                checkBoxLampList.add(lampList.get(pos));
                                            }

                                        }
                                    }
                                }

                                @Override
                                public void onSelectedDataCallBack(GroupAdapter.Light bean) {

                                }
                            });
                        } else {
                            lightAdapter.setListAll(lampList);
                            lightAdapter.notifyDataSetChanged();
                        }

                    }
                    Toast.makeText(ControlActivity2.this, "获取所有灯具信息成功", Toast.LENGTH_SHORT).show();


                }

                @Override
                public void onFailure(Call<LampInterfaceBean<ArrayList<LampListBean>>> call, Throwable t) {
                    SCLog.e(TAG, "response is onFailure" + call.toString());
                    Toast.makeText(ControlActivity2.this, "获取所有灯具信息失败" + t.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    public void refreashLight() {
        for (int i = 0; i < lampList.size(); i++) {
            checkBoxlist.get(i).setVisibility(View.VISIBLE);
            imageViewList.get(i).setVisibility(View.VISIBLE);
        }
    }

    //区域选择提示
    private RegionBean nowRegion;

    private void getRegion() {
        try {
            String url = StringUtils.buildURL("http", host, port, "", null).toString();
            String token = getConfig().getString(ConfigurationEntry.TOKEN, "");

            RetrofitWrapper.getInstance(ControlActivity2.this, url).create(AuthManageService.class).getRegion(token).enqueue(new Callback<AuthBaseBean<ArrayList<RegionBean>>>() {
                @Override
                public void onResponse(Call<AuthBaseBean<ArrayList<RegionBean>>> call, Response<AuthBaseBean<ArrayList<RegionBean>>> response) {
                    SCLog.e(TAG, "response is onResponse" + call.toString());
                    if (response != null && response.body() != null && response.body().getData() != null && response.body().getData().size() != 0) {

                        if (nowRegion == null) {
                            nowRegion = response.body().getData().get(0);
                        }
                    } else {
                        Toast.makeText(ControlActivity2.this, "获取区域信息失败", Toast.LENGTH_SHORT).show();
                    }

                }

                @Override
                public void onFailure(Call<AuthBaseBean<ArrayList<RegionBean>>> call, Throwable t) {
                    SCLog.e(TAG, "response is onFailure" + call.toString());
                    Toast.makeText(ControlActivity2.this, "获取区域信息失败", Toast.LENGTH_SHORT).show();
                }
            });
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }


    //加载当前灯具位置信息
    public void setLamp() {
        for (int i = 0; i < lampList.size(); i++) {
            if (textViewList.size() > i) {
                String snId = lampList.get(i).getLampSn();
                textViewList.get(i).setText(snId);
            }
        }
    }

    Handler handler = new Handler();

    //根据亮灯状态 决定是否更新策略
    public void refreashWays() {
        if (System.currentTimeMillis() - lastWaysTime > 2000) {
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    /**
                     *要执行的操作
                     */
                    add4TimeAndMan(groupAdapter.getSelectedGroupId());
                }
            }, 2000);
        } else {

        }
    }

    BroadcastReceiver my = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(LIGHT_OPRATE_ACTION)) {
                MessageModel model = (MessageModel) intent.getSerializableExtra(LIGHT_OPRATE_ACTION_EXTRA_DATA);
                getLampList();
                if (lightAdapter.getLightState()) {
                    iv_light_open.setSelected(true);
                } else {
                    iv_light_open.setSelected(false);
                }
//                if (model.getType().equals("region")) {
//                    int id = (int) (model.getData());
//                    String state = model.getSource();
//                    if (id == Integer.valueOf(nowRegion.getValue())) {
//                        if(state.equals("1")){
//                            if()
//                        }else{
//
//                        }
//                    }
//                } else if (model.getType().equals("group")) {
//
//                }
            }
        }
    };
}